﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace WpfChartingControlLibrary.Common
{
    /// <summary>
    /// used to help control the lifetime of the Grid sizechanged event
    /// </summary>
    public class GridSizeChangedEventManager : WeakEventManager
    {
        public static GridSizeChangedEventManager CurrentEventManager
        {
            get
            {
                var manager_type = typeof(GridSizeChangedEventManager);
                var manager = WeakEventManager.GetCurrentManager(manager_type) as GridSizeChangedEventManager;

                if (manager == null)
                {
                    manager = new GridSizeChangedEventManager();
                    WeakEventManager.SetCurrentManager(manager_type, manager);
                }

                return manager;
            }
        }

        public static void AddListener(Grid source, IWeakEventListener listener)
        {
            CurrentEventManager.ProtectedAddListener(source, listener);
        }

        public static void RemoveListener(Grid source, IWeakEventListener listener)
        {
            CurrentEventManager.ProtectedRemoveListener(source, listener);
        }

        protected override void StartListening(object source)
        {
            ((Grid)source).SizeChanged += DeliverEvent;
        }

        protected override void StopListening(object source)
        {
            ((Grid)source).SizeChanged -= DeliverEvent;
        }
    }

}
